/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package libraryproject;

import database_objects.Author;
import database_objects.Book;
import database_objects.Department;
import database_objects.EditionBook;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import user_client.LibrarianUserClient;

/**
 *
 * @author pawel
 */
public class BooksManagementsPane extends javax.swing.JPanel {

    /**
     * Creates new form BooksManagementsPane
     */
    public BooksManagementsPane() {
        initComponents();

        authorsList.setModel(authorsListModel);
    }

    public BooksManagementsPane(LibrarianUserClient client) {
        initComponents();
        this.client = client;
        authorsListModel = new DefaultListModel();
        authorsList.setModel(authorsListModel);


    }

    public void setAuthors(ArrayList<Author> authors) {
        this.authors.clear();
        for (Author author : authors) {
            System.out.println(author.toString());
            authorsListModel.addElement(author);
            this.authors.add(author);
        }
        System.out.println("aktualizacja");
    }

    public void setEdition(EditionBook edition, Department department) {
        isbnField.setText(edition.getIsbn());
        titleField.setText(edition.getTitle());
        editionField.setText(edition.getNameOfEditionHouse());
        dateField.setText(edition.getDateOfPublishing());
        setDepartment(department);

        currentEdition = edition;
    }

    public void setDepartment(Department department) {
        this.caseNumberField.setText(department.getBookCaseNumber());
        this.departmentNameField.setText(department.getName());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        authorsList = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        departmentNameField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        caseNumberField = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        positioinFormatedField = new javax.swing.JFormattedTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        isbnField = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        editionField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        dateField = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        titleField = new javax.swing.JTextField();
        choseEditionButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(500, 363));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "isbn", "tytuł", "autor", "wydawnictwo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 640, 150));

        jPanel5.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Autorzy:"));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton3.setText("Wybierz autorów");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 245, 146, 26));

        jScrollPane2.setViewportView(authorsList);

        jPanel4.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 19, 146, 211));

        jPanel5.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 170, 280));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("wybierz dział"));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel5.setText("nazwa:");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 21, 148, 26));
        jPanel2.add(departmentNameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 49, 148, 26));

        jLabel6.setText("numer regału:");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 77, 148, 26));
        jPanel2.add(caseNumberField, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 105, 148, 26));

        jButton2.setText("wybierz dział");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 245, 148, 26));

        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel2.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, 140, -1));

        jLabel7.setText("pozycja na półce:");
        jPanel2.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, -1, -1));
        jPanel2.add(positioinFormatedField, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 140, -1));

        jPanel5.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 0, 160, 280));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("wydanie"));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("isbn:");
        jPanel3.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 21, 148, 26));
        jPanel3.add(isbnField, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 49, 148, 26));

        jLabel2.setText("wydawnictwo:");
        jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 77, 148, 26));

        editionField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editionFieldActionPerformed(evt);
            }
        });
        jPanel3.add(editionField, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 105, 148, 26));

        jLabel3.setText("data wydania:");
        jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 133, 148, 26));
        jPanel3.add(dateField, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 161, 148, 26));

        jLabel4.setText("tytuł:");
        jPanel3.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 189, 148, 26));
        jPanel3.add(titleField, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 217, 148, 26));

        choseEditionButton.setText("wybierz wydanie");
        choseEditionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choseEditionButtonActionPerformed(evt);
            }
        });
        jPanel3.add(choseEditionButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 245, 148, 26));

        jPanel5.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 0, 160, 280));

        jButton4.setText("dodaj książkę");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("usuń książkę");

        searchButton.setText("szukaj");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchButton)
                .addContainerGap(155, Short.MAX_VALUE))
        );

        jPanel5.add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 10, 160, -1));

        add(jPanel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 160, 640, 276));
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try {

            
            ArrayList<EditionBook> editions = client.searchEditions(
                    jFormattedISBNField.getText(),
                    editionNameTextField.getText(),
                    dateOfEditionTextField.getText(),
                    titleField.getText(),
                    idOfDepartmentString);

            while (model.getRowCount() > 0) {
                try {
                    model.removeRow(0);
                } catch (Exception a) {
                    System.out.println("ERRRORRRRRRRR????");
                }

            }

            System.out.println("SIZE: " + editions.size());
            for (EditionBook edition : editions) {
                System.out.println(edition.getNameOfEditionHouse());
                String[] val = new String[5];
                val[0] = edition.getIsbn();
                val[1] = edition.getTitle();
                val[2] = edition.getNameOfEditionHouse();
                val[3] = edition.getDateOfPublishing();
                val[4] = edition.getIdOfDepartment();
                model.addRow(val);
            }

            resultTable.revalidate();

            resultTable.repaint();

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }        // T

        // TODO add your handling code here:
    }//GEN-LAST:event_searchButtonActionPerformed

    private void choseEditionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_choseEditionButtonActionPerformed
        JDialog frame = new JDialog();
        currentEdition = new EditionBook(isbnField.getText(),
                editionField.getText(), dateField.getText(),
                titleField.getText(), null);
        EditionChoicePane edition = new EditionChoicePane(
                client, currentBook, this, frame, currentEdition);
        frame.add(edition);
        frame.setVisible(true);
        frame.setSize(edition.getPreferredSize());
        // TODO add your handling code here:
    }//GEN-LAST:event_choseEditionButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JDialog frame = new JDialog();
        System.out.println(client);
        DepartmentChoicePane edition = new DepartmentChoicePane(client,
                currentBook, this, frame);
        frame.add(edition);
        frame.setVisible(true);
        frame.setSize(edition.getPreferredSize());
    }//GEN-LAST:event_jButton2ActionPerformed

    private void editionFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editionFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_editionFieldActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        JDialog frame = new JDialog();
        AuthorsPane authorsPane = new AuthorsPane(client, currentBook, this, frame, authors);
        frame.add(authorsPane);
        frame.setVisible(true);
        frame.setSize(authorsPane.getPreferredSize());        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged

        positioinFormatedField.setText(jComboBox1.getSelectedItem().toString());
    }//GEN-LAST:event_jComboBox1ItemStateChanged
    private LibrarianUserClient client;
    private Book currentBook = new Book();
    private ArrayList<Author> authors = new ArrayList<Author>();
    private DefaultListModel authorsListModel = null;
    private EditionBook currentEdition = null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList authorsList;
    private javax.swing.JTextField caseNumberField;
    private javax.swing.JButton choseEditionButton;
    private javax.swing.JFormattedTextField dateField;
    private javax.swing.JTextField departmentNameField;
    private javax.swing.JTextField editionField;
    private javax.swing.JFormattedTextField isbnField;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JFormattedTextField positioinFormatedField;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField titleField;
    // End of variables declaration//GEN-END:variables
}
